//
//  CustomElemCmd.m
//  TIMChat
//
//  Created by wilderliao on 16/6/22.
//  Copyright © 2016年 AlexiChen. All rights reserved.
//

#import "CustomElemCmd.h"
#import "NSDictionary+Ext.h"
#import "NSObject+Json.h"
#import "JSONKit.h"

@implementation CustomElemCmd

- (instancetype)initWith:(NSInteger)command
{
    if (self = [super init])
    {
        _action = command;
    }
    return self;
}

- (instancetype)initWith:(NSInteger)command param:(NSString *)param
{
    if (self = [super init])
    {
        _action = command;
        _videoUrl = param;
    }
    return self;
}
- (instancetype)initWith:(NSInteger)command chatQuestion:(NSString *)chatQuestion
{
    if (self = [super init])
    {
        _action = command;
        _chatQuestion = chatQuestion;
    }
    return self;
}
- (instancetype)initWith:(NSInteger)userACtion urlStr:(NSString *)urlStr priceStr:(NSString *)priceStr imageStr:(NSString *)imageStr withPayId:(NSString *)payID{
    if (self = [super init])
    {
        _action = userACtion;
        _videoUrl = urlStr;
        _price = priceStr;
        _imageUrl = imageStr;
        _payId = payID;
    }
    return self;
    
}
- (instancetype)initWithGift:(NSInteger)userACtion priceStr:(NSString *)priceStr giftBubbleImageUrl:(NSString *)giftBubbleImage giftMsgImageUrl:(NSString *)giftMsgImage NameStr:(NSString *)nameStr{
    if (self = [super init])
    {
        _action = userACtion;
        _price = priceStr;
        _giftBubbleImage = giftBubbleImage;
        _giftMsgImage = giftMsgImage;
        _giftName = nameStr;
        
        
    }
    return self;
    
}

+ (NSDictionary *)modelCustomPropertyMapper {
    return @{@"customInfo" : @"CustomParam",
             };
}

+ (instancetype)parseCustom:(TIMCustomElem *)elem
{
    NSData *data = elem.data;
    if (data)
    {
        NSString *dataStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        NSLog(@"gggggg === %@", dataStr);
        CustomElemCmd *parse = [NSObject parse:[CustomElemCmd class] jsonString:dataStr];
        //传输newRecodeId
        CustomElemCmd *parseParam = [CustomElemCmd modelWithJSON:dataStr];
        if(ValidDict(parseParam.customInfo))
        {
            [kUserDefaults setObject:parseParam.customInfo forKey:KPOSTNEWRECODEID];
        }
//
        //        if (parse.msgType == EIMAMSG_InputStatus || parse.msgType == EIMAMSG_SaftyTip)
        //        {
        parse.customInfo = [parse.imageUrl objectFromJSONString];
        parse.videoPhoneMessageDic = [parse.videoPhoneMessage objectFromJSONString];
        return parse;
        //        }
    }
    
    DebugLog(@"自定义消息不是CustomElemCmd类型");
    return nil;
    
}

- (NSData *)packToSendData
{
    NSMutableDictionary *post = [NSMutableDictionary dictionary];
    [post setObject:@(_action) forKey:@"action"];
    
    if (_videoUrl && _videoUrl.length > 0)
    {
        [post setObject:_videoUrl forKey:@"videoUrl"];
    }
    if (_chatQuestion && _chatQuestion.length > 0)
    {
        [post setObject:_chatQuestion forKey:@"chatQuestion"];
    }
    if (_price && _price.length > 0)
    {
        [post setObject:_price forKey:@"price"];
    }
    if (_imageUrl && _imageUrl.length > 0)
    {
        [post setObject:_imageUrl forKey:@"imageUrl"];
    }
    if (_giftBubbleImage && _giftBubbleImage.length > 0)
    {
        [post setObject:_giftBubbleImage forKey:@"giftBubbleImage"];
    }
    if (_giftMsgImage && _giftMsgImage.length > 0)
    {
        [post setObject:_giftMsgImage forKey:@"giftMsgImage"];
    }
    if (_giftName && _giftName.length > 0)
    {
        [post setObject:_giftName forKey:@"giftName"];
    }
    if (_payId && _payId.length > 0)
    {
        [post setObject:_payId forKey:@"payId"];
    }
    
    if ([NSJSONSerialization isValidJSONObject:post])
    {
        NSError *error = nil;
        NSData *data = [NSJSONSerialization dataWithJSONObject:post options:NSJSONWritingPrettyPrinted error:&error];
        if(error)
        {
            DebugLog(@"[%@] Post Json Error: %@", [self class], post);
            return nil;
        }
        
        DebugLog(@"CustomElemCmd content is %@", post);
        return data;
    }
    else
    {
        DebugLog(@"[%@] CustomElemCmd is not valid: %@", [self class], post);
        return nil;
    }
}

- (void)prepareForRender
{
    // 因不用于显示，作空实现
    // do nothing
}

- (NSInteger)msgType
{
    return _action;
}
//视频聊相关
- (instancetype)initFaceWith:(NSInteger)userACtion cmd:(int)cmdTag rejectIdStr:(NSString *)rejectIdStr callIdStr:(NSString *)callIdStr {
    if (self = [super init])
    {
        _action = userACtion;
//        _command = cmdTag;
//        _rejectId = rejectIdStr;
//        _callId = callIdStr;
    }
    return self;
}
- (id)parseFaceCustom:(TIMCustomElem *)elem {
    NSDictionary * otherInfoDic;
    
    if (self) {
        otherInfoDic  =[NSDictionary dictionaryWithJsonString:[[NSString alloc] initWithData:elem.data  encoding:NSUTF8StringEncoding]];
        self.action = [otherInfoDic[@"action"] intValue];
        self.videoPhoneMessage = otherInfoDic[@"videoPhoneMessage"];
        if ([self.videoPhoneMessage isKindOfClass:[NSDictionary class]]) {
            self.command = [otherInfoDic[@"videoPhoneMessage"][@"command"] intValue];
            self.callId = otherInfoDic[@"videoPhoneMessage"][@"callId"];
            self.rejectId = otherInfoDic[@"videoPhoneMessage"][@"rejectId"];

        }else {
            if (![self.videoPhoneMessage isEqualToString:@""]  && self.videoPhoneMessage.length > 0 && ![self.videoPhoneMessage isEqualToString:@"null"]) {
                self.command = [[NSDictionary dictionaryWithJsonString:self.videoPhoneMessage][@"command"] intValue];
                self.callId = [NSDictionary dictionaryWithJsonString:self.videoPhoneMessage][@"callId"];
                self.rejectId = [NSDictionary dictionaryWithJsonString:self.videoPhoneMessage][@"rejectId"];

            }

        }
        NSLog(@"lllll === %ld ==== %@",(long)self.action, self.self.callId);
    }
    return self;
}
+ (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString
{
    if (jsonString == nil) {
        return nil;
    }
    
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
                                                        options:NSJSONReadingMutableContainers
                                                          error:&err];
    if(err)
    {
        NSLog(@"json解析失败：%@",err);
        return nil;
    }
    return dic;
}



- (NSData *)faceTofaceSendData {
    NSMutableDictionary *post = [NSMutableDictionary dictionary];
    [post setObject:@(_action) forKey:@"action"];
    NSMutableDictionary *MessageDic = [NSMutableDictionary dictionary];
;
    if (_command > 0)
    {
        if (_command > 0) {
            [MessageDic setObject:@(_command) forKey:@"command"];
            
        }
        if (_callId && _callId.length > 0)
        {
            [MessageDic setObject:_callId forKey:@"callId"];
        }
        if (_rejectId && _rejectId.length > 0)
        {
            [MessageDic setObject:_rejectId forKey:@"rejectId"];
        }
        
        [post setObject:[self dictionaryToJson:MessageDic] forKey:@"videoPhoneMessage"];
    }
    NSLog(@"ffffffffcccccc ---== %@",post);
    if ([NSJSONSerialization isValidJSONObject:post])
    {
        NSError *error = nil;
        NSData *data = [NSJSONSerialization dataWithJSONObject:post options:NSJSONWritingPrettyPrinted error:&error];
        if(error)
        {
            DebugLog(@"[%@] Post Json Error: %@", [self class], post);
            return nil;
        }
        
        DebugLog(@"CustomElemCmd content is %@", post);
        return data;
    }
    else
    {
        DebugLog(@"[%@] CustomElemCmd is not valid: %@", [self class], post);
        return nil;
    }
}
-(NSString*)dictionaryToJson:(NSDictionary *)dic

{
    
    NSError *parseError = nil;
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&parseError];
    
    return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    
}

@end
